<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="'/home'">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="mt">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="14">
          <el-button type="primary" @click="dialogFormVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格 -->
    <template>
      <el-table :data="userlist" border style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="create_time" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="address" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">

          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click='del(scope.row.id)'></el-button>
          <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--  -->

    <!-- 添加弹出框 -->

    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules">
        <el-form-item label="用户" :label-width="formLabelWidth" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="mobile">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
    <!--  -->


    <!-- 编辑弹框 -->
           <el-dialog title="编辑用户" :visible.sync="dialogVisiblebj">
      <el-form :model="edito" :rules="rulesbj">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="edito.username" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
          <el-input v-model="edito.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="mobile">
          <el-input v-model="edito.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addbj(edito)">确 定</el-button>
      </div>
    </el-dialog>
    <!--  -->

    <!-- 分页 -->
    <div class="mt">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.pagenum"
        :page-sizes="[3, 6, 9]"
        :page-size="tableData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { Listuser } from "@/http/api";
import {addUser} from '@/http/api'
import {delUser} from '@/http/api'
import {eDit} from '@/http/api'
export default {
  data() {
    return {
      input: "",
      tableData: {
        query: "",
        pagenum: 1,
        pagesize: 3,
      },
      userlist: [],
      total: 0,
      dialogFormVisible: false,
      dialogVisiblebj: false,
      //添加的对象
      form: {
          username:'',
          password:'',
          email:'',
          mobile:''

      },

      formLabelWidth: "120px",
      //添加
      rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 8, message: '长度在 3 到 8个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱号', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { min: 3, max: 11, message: '长度在 3 到 11 个字符', trigger: 'blur' }
          ],
        },
        //编辑的对象
        edito:{
          username:'',
          email:'',
          mobile:''
        },
        //编辑
         rulesbj: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            
          ],
          email: [
            { required: true, message: '请输入邮箱号', trigger: 'blur' },
          ],
          mobile: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
          ],
        }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //删除单个数据
    async del(id){
      const res=await delUser(id)
      console.log('删除单个数据',res);
      this.getList();
    },
    //编辑用户并提交
    async addbj(id){
        const res=await eDit(this.edito)
        console.log('编辑用户',res);
        this.getList();
    },
    //编辑提交用户,线获取数据
     edit(row){
        this.dialogVisiblebj = true
        this.edito=row
        
    },
    //添加用户
    async add(){
        var obj={
            username:this.form.username,
            password:this.form.password,
            email:this.form.email,
            mobile:this.form.mobile
        }
        const res=await addUser(obj)
        console.log('添加用户',res);
        this.getList();
        this.dialogFormVisible=false,
        this.form.username=''
        this.form.password=''
        this.form.email=''
        this.form.mobile=''
    },
    //调用用户列表数据
    async getList() {
      const res = await Listuser(this.tableData);
      console.log("用户列表数据", res);
      let { total, users } = res;
      this.userlist = users;
      this.total = total;
    },
    handleSizeChange(val) {
      this.tableData.pagesize = val;
      this.getList();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.tableData.pagenum = val;
      this.getList();
      // console.log(`当前页: ${val}`);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-row {
  text-align: left;
  
}
</style>
